package top.lingkang.finalgateway.admin.task;

import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.scheduling.annotation.Scheduled;
import top.lingkang.finalgateway.FinalGateway;
import top.lingkang.finalgateway.admin.mapper.AccessLogMapper;
import top.lingkang.finalgateway.admin.mapper.IpListLogMapper;
import top.lingkang.finalgateway.admin.vo.RateIpVO;
import top.lingkang.finalgateway.core.NettyService;
import top.lingkang.finalgateway.core.tcp.TcpService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lingkang
 * Created by 2024/5/19
 */
@Component
@Slf4j
public class MonitorTask {

    @Inject
    private IpListLogMapper ipListLogMapper;
    @Inject
    private AccessLogMapper accessLogMapper;

    /**
     * 60 分钟执行一次
     */
    @Scheduled(fixedRate = 3600000, initialDelay = 60000)
    public void refreshMonitor() {
        log.info("开始定时刷新缓存");
        FinalGateway.refreshMonitor();
        log.info("定时刷新缓存完成");
    }

    /**
     * 2 分钟执行一次
     */
    @Scheduled(fixedRate = 120000, initialDelay = 120000)
    public void accessMonitor() {
        Date time = new Date(System.currentTimeMillis() - 120000);
        accessMonitorHttp(time, null);
        accessMonitorTcp(time, null);
    }

    public synchronized void accessMonitorHttp(Date start, Integer port) {
        for (Map.Entry<Integer, NettyService> entry : FinalGateway.service.entrySet()) {
            if (!entry.getValue().config.getAccessCheck().isEnable())
                continue;
            if (port != null && !port.equals(entry.getKey()))
                continue;
            List<RateIpVO> list = accessLogMapper.getGreaterRateIp(
                    entry.getValue().config.getPort(),
                    start,
                    entry.getValue().config.getAccessCheck().getRate()
            );
            if (list.isEmpty())
                continue;
            for (RateIpVO vo : list)
                if (!entry.getValue().config.getIpListCheck().isWhiteIp(vo.getIp())) {
                    entry.getValue().config.getIpListCheck().addBlack(vo);
                }
        }
    }

    public synchronized void accessMonitorTcp(Date start, Integer port) {
        for (Map.Entry<Integer, TcpService> entry : FinalGateway.tcpService.entrySet()) {
            if (!entry.getValue().config.getAccessCheck().isEnable())
                continue;
            if (port != null && !port.equals(entry.getKey()))
                continue;
            List<RateIpVO> list = accessLogMapper.getGreaterRateIp(
                    entry.getValue().config.getPort(),
                    start,
                    entry.getValue().config.getAccessCheck().getRate()
            );
            if (list.isEmpty())
                continue;
            for (RateIpVO vo : list)
                if (!entry.getValue().config.getIpListCheck().isWhiteIp(vo.getIp())) {
                    entry.getValue().config.getIpListCheck().addBlack(vo);
                }
        }
    }

}
